// use log::info;
use itertools::Itertools;
use crate::block::BLOCKS;

// 二值化
pub fn grid_to_bin(grid: &Vec<u8>, n: usize) -> Vec<Vec<u8>> {
    let mut ret = vec![vec![0; n]; n];
    for i in 0..n {
        for j in 0..n {
            let tb = grid[i * n + j];
            ret[i][j] = if tb > 0 { 1 } else { 0 };
        }
    }
    ret
}

// 二值矩阵中放置块
pub fn matrix_put_down(a: &mut Vec<Vec<u8>>, bid: u8, x: u8, y: u8) {
    let blk = &BLOCKS[bid as usize - 1];
    for i in 0..blk.rect.height as u8 {
        for j in 0..blk.rect.width as u8 {
            let gy = (i + blk.rect.y as u8) as usize;
            let gx = (j + blk.rect.x as u8) as usize;
            let sy = (y + i) as usize;
            let sx = (x + j) as usize;
            if blk.grid[gy][gx] != 0 {
                a[sy][sx] = blk.grid[gy][gx];
            }
        }
    }
}

//0-1之前 越接近1 越分散
#[allow(dead_code)]
pub fn entropy(matrix: &mut Vec<Vec<u8>>) -> f64 {
    let mut freq_array = vec![0.0; 64]; // 长度为64的数组，初始化为0

    let total_elements = (matrix.len() * matrix[0].len()) as f64;

    // 计算每个元素的频率
    for row in matrix.iter() {
        for &element in row.iter() {
            freq_array[element as usize] += 1.0;
        }
    }

    // 计算熵
    freq_array.iter().fold(0.0, |acc, &freq| {
        if freq > 0.0 {
            let p = freq / total_elements;
            acc - p * p.log2()
        } else {
            acc
        }
    })
}

// 计算最大矩形面积和消除行数
pub fn matrix_calc(a: &mut Vec<Vec<u8>>, n: usize, m: usize) -> (i32, u8) {
    let mut fill_col = 0;
    let mut fill_row = 0;

    for i in 1..n {
        let mut clear_row = true;
        for j in 0..m {
            if a[i][j] == 1 && a[i - 1][j] != 0 {
                a[i][j] += a[i - 1][j];
                if a[i][j] == n as u8 {
                    fill_col += 1;
                }
            }
            clear_row = clear_row && a[i][j] > 0;
        }
        if clear_row {
            fill_row += 1;
        }
    }

    let mut max_area;
    let mut mv = vec![];
    for i in 0..n {
        let mut s = Vec::new();
        let mut j = 1;
        // let mut max_temp = a[i][0];
        s.push(0);

        while j < m || (j == m && !s.is_empty()) {
            if j != m && (s.is_empty() || a[i][j] >= a[i][*s.last().unwrap()]) {
                s.push(j);
                j += 1;
            } else {
                let top_num = a[i][*s.last().unwrap()] as i32;
                s.pop();
                let curr_max = if !s.is_empty() {
                    top_num * ((j - *s.last().unwrap() - 1) as i32)
                } else {
                    top_num * (j as i32)
                };
                if curr_max > 5 {
                    mv.push(curr_max);
                }
                // max_temp = max_temp.max(curr_max as u8);
            }
        }
        // max_area = max_area.max(max_temp);
    }

    let mvv:Vec<_> = mv.into_iter().unique().collect();

    // mvv.sort_by(|a, b| b.cmp(a));
    max_area = 0;
    for m in &mvv {
        max_area += m * m;
    }
    // info!("mv...{:?} {:?}", max_area, mvv);

    (max_area, fill_row + fill_col)
}
